package com.tarena.lbs.stock.web.service;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tarena.lbs.pojo.stock.dos.CouponStockDO;
import com.tarena.lbs.pojo.stock.dto.CouponStockDTO;
import com.tarena.lbs.pojo.stock.param.CouponStockParam;
import com.tarena.lbs.pojo.stock.po.CouponStockPO;
import com.tarena.lbs.stock.web.repository.CouponStockRepository;
import com.tarena.lbs.stock.web.repository.StockRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StockService {
    @Autowired
    private CouponStockRepository couponStockRepository;
    @Autowired
    private StockRepository stockRepository;

    public boolean saveCouponStock(CouponStockParam param) {
        //1.封装 po
        CouponStockPO poParam=new CouponStockPO();
        BeanUtils.copyProperties(param,poParam);
        poParam.setCreateAt(new Date());
        poParam.setUpdateAt(poParam.getCreateAt());
        //2.数据层保存写入
        int result=couponStockRepository.saveCouponStock(poParam);
        //注意: mybatis对于写操作 会自动将影响的行数 做返回值处理 在没有出现连接异常时,
        //执行的写操作可以利用返回值>0判断成功
        return result>0;
    }

    public boolean updateCouponNum(Integer couponId) {
        return stockRepository.updateCouponNum(couponId);

    }

    public List<CouponStockDTO> getCouponNum(List<Integer> ids) {
        List<CouponStockDO> dos = stockRepository.getCouponNum(ids);
        if (CollectionUtils.isNotEmpty(dos)){
           return dos.stream().map(Do->{
                CouponStockDTO dto = new CouponStockDTO();
                BeanUtils.copyProperties(Do,dto);
                return dto;
            }).collect(Collectors.toList());
        }
       return null;
    }
}
